﻿using System;
using Opo.Net.Mail;
using System.Xml.Serialization;

namespace Opo.Perspective.Core
{
    /// <summary>
    /// Represents a mail message
    /// </summary>
    [XmlRoot("mailMessage")]
    public class Message : MailMessage, IMailMessage, IPerspectiveEntity
    {
        private string _title;
        private TagCollection _tags = new TagCollection();

        /// <summary>
        /// Gets or sets the unique ID (server independent).
        /// </summary>
        /// <value>The unique ID.</value>
        public Guid ID { get; set; }
        /// <summary>
        /// Gets or sets the title. If no title is specified the mail message's subject is returned.
        /// </summary>
        /// <value>The title.</value>
        [XmlIgnore]
        public string Title
        {
            get
            {
                if (String.IsNullOrEmpty(_title))
                {
                    return Subject;
                }
                else
                {
                    return _title;
                }
            }
            set
            {
                _title = value;
            }
        }
        /// <summary>
        /// Gets the body of the mail message
        /// </summary>
        /// <value>The body of the mail message.</value>
        [XmlIgnore]
        public string Summary
        {
            get
            {
                return Body;
            }
        }
        /// <summary>
        /// Gets or sets a flag for the mail message.
        /// </summary>
        /// <value>A flag.</value>
        [XmlElement("flag")]
        public IFlag Flag { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this entity has been read.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this entity has been read; otherwise, <c>false</c>.
        /// </value>
        [XmlElement("isRead")]
        public bool IsRead { get; set; }
        /// <summary>
        /// Gets or sets a tags collection.
        /// </summary>
        /// <value>A collection of tags</value>
        [XmlArray(ElementName = "tags")]
        [XmlArrayItem(ElementName = "tag", IsNullable = true)]
        public TagCollection Tags
        {
            get
            {
                return _tags;
            }
            set
            {
                _tags = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerspectiveMailMessage"/> class.
        /// </summary>
        public Message()
        {
            ID = Guid.NewGuid();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PerspectiveMailMessage"/> class.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="from">The sender's <see cref="IMailAddress">IMailAddress</see>.</param>
        /// <param name="to">A <see cref="">MailAddressCollection</see> containing the reciever's <see cref="IMailAddress">MailAddress</see>.</param>
        /// <param name="body">The body of the message</param>
        /// <param name="bodyType">Type of the body.</param>
        public Message(string subject, IMailAddress from, MailAddressCollection to, string body, MailMessageBodyType bodyType)
            : this()
        {
            Subject = subject;
            From = from;
            To = to;
            Body = body;
            BodyType = bodyType;
        }
    }
}